package com.gitee.aurora.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * 类描述: 配置Swagger2 页面
 *
 * @author wanghongchao
 * @version 1.0
 * @date 2019/09/16 11:39
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    /**
    *
    * 功能描述: 创建swagger api
    *
    * @return springfox.documentation.spring.web.plugins.Docket docket
    * @author wanghongchao
    * @date 2022/9/19 14:48
    */
    @Bean
    public Docket createRestApi() {
        List<Parameter> pars = new ArrayList<Parameter>();

        ParameterBuilder sysIdPar = new ParameterBuilder();
        sysIdPar.name("token").description("登录令牌")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        pars.add(sysIdPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.gitee"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars)
                .apiInfo(apiInfo());
    }

    /**
    *
    * 功能描述: api配置信息对象
    *
    * @return springfox.documentation.service.ApiInfo ApiInfo
    * @author wanghongchao
    * @date 2018/11/7 14:49
    */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("DEMO API文档")
                .description("文档描述DEMO提供的所有 REST 接口")
                .version("1.0")
                .build();
    }
}
